package com.shuizhu.template.demo3;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.shuizhu.template.StudentByFill;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class HorizontalFill {
  
  public static void main(String[] args) {
    //要填充的Excel模板的路径
    String templatePath = "F:\\JavaCodeStandalone\\easy-excel-study\\eaey-excel-basic\\src\\main\\resources\\excel_template\\水平列表填充模板.xlsx";
    //填充后文件的保存路径
    String savePath = "F:\\JavaCodeStandalone\\easy-excel-study\\eaey-excel-basic\\src\\main\\resources\\excel\\水平列表填充-Write.xlsx";
    //根据模板获取工作簿对象
    ExcelWriter workBook = EasyExcel
        .write(savePath, StudentByFill.class)
        .withTemplate(templatePath)
        .build();
    //获取工作表对象
    WriteSheet sheet = EasyExcel.writerSheet().build();
    
    //构建一个 FillConfig 对象，设置填充方向
    FillConfig build = FillConfig.builder()
        //设置填充方向为水平
        .direction(WriteDirectionEnum.HORIZONTAL)
        .build();
    
    //填充数据
    workBook.fill(getFillDataList(), build, sheet);
    //调用do开头的方法时，内部会自动关闭流，当调用fill()方法时，需要自己手动关闭流
    workBook.finish();
  }
  
  /**
   * 生成要用于填充的数据
   */
  public static List<StudentByFill> getFillDataList() {
    List<StudentByFill> list = new ArrayList<>();
    
    for (int i = 1; i <= 10; i++) {
      list.add(new StudentByFill(String.valueOf((int) (Math.random() * 1000_0000) + 1000_0000), "张" + i, new Date(), Math.random() < 0.5 ? "男" : "女", Math.random() * 1_0000, "信息工程学院"));
    }
    return list;
  }
  
}

